<template>
  <div id="detail">
    <detail-nav-bar
      class="detail-nav"
      @titleClick="titleClick"
      ref="nav"
    ></detail-nav-bar>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probeType="3">
      <detail-swiper :topImages="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-param-info ref="param" :paramInfo="paramInfo"></detail-param-info>
      <detail-comment-info
        ref="comment"
        :commentInfo="commentInfo"
      ></detail-comment-info>
      <goods-list ref="recommend" :goods="recommends"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar @addCart="addToCart"></detail-bottom-bar>
    <!-- <toast :message="message" :show="show"></toast> -->
  </div>
</template>

<script>
import DetailNavBar from "./ChildComponents/DetailNavBar.vue";
import DetailSwiper from "./ChildComponents/DetailSwiper.vue";
import DetailBaseInfo from "./ChildComponents/DetailBaseInfo.vue";
import DetailShopInfo from "./ChildComponents/DetailShopInfo.vue";
import DetailGoodsInfo from "./ChildComponents/DetailGoodsInfo.vue";
import DetailParamInfo from "./ChildComponents/DetailParamInfo.vue";
import DetailCommentInfo from "./ChildComponents/DetailCommentInfo.vue";
import DetailBottomBar from "./ChildComponents/DetailBottomBar.vue";

import Scroll from "components/common/scroll/Scroll";
import GoodsList from "components/content/goods/GoodsList";
import BackTop from "components/content/backTop/BackTop";
// import Toast from "components/common/toast/Toast"

import { mapActions } from 'vuex'

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail";
import { itemListenerMixin, BackTopMixin } from "common/mixin";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    Scroll,
    GoodsList,
    DetailBottomBar,
    BackTop,
    // Toast
  },

  mixins: [itemListenerMixin, BackTopMixin],

  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      // message: '',
      // show: false
    };
  },

  created() {
    //1. 保存传入的iid
    this.iid = this.$route.params.iid;

    //2. 根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      console.log(res);
      const data = res.data.result; //改变一下获取数据的路径嘛  把res.data.result简化成一个data
      //1. 获取顶部的图片轮播数据
      this.topImages = data.itemInfo.topImages;

      //2. 获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      //3. 创建店铺信息
      this.shop = new Shop(data.shopInfo);

      //4. 保存商品的详情数据
      this.detailInfo = data.detailInfo;

      //5. 获取参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      //6. 获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }

      // this.$nextTick(() => {
      //   //这个东西还是会有bug的
      //   // 根据最新的数据，对应的dom是已经被渲染出来
      //   // 但是图片依然没有加载完（目前获取到offsetTop不包含其中的图片）
      //   // offsetTop值不对的时候， 都是因为图片的问题。
      //   this.themeTopYs = [];

      //   this.themeTopYs.push(0);
      //   this.themeTopYs.push(this.$refs.param.$el.offsetTop);
      //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      //   this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      //   console.log(this.themeTopYs);
      // });
    });

    //3. 请求推荐数据
    getRecommend().then((res) => {
      // console.log(res);
      this.recommends = res.data.data.list;
      // console.log(this.recommends);
    });

    //4. 给getThemeTopY赋值
  },

  mounted() {},

  updated() {},

  destroyed() {
    this.$bus.$off("itemImgLoad", this.itemImgListener);
  },

  methods: {
    ...mapActions(['addCart']),  //把vuex里面的actions里面的addCart方法映射过来
    imageLoad() {
      this.$refs.scroll.refresh();

      this.$nextTick(() => {
        //这个东西还是会有bug的
        // 根据最新的数据，对应的dom是已经被渲染出来
        // 但是图片依然没有加载完（目前获取到offsetTop不包含其中的图片）
        // offsetTop值不对的时候， 都是因为图片的问题。
        this.themeTopYs = [];

        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.param.$el.offsetTop);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
        this.themeTopYs.push(Number.MAX_VALUE);

        console.log(this.themeTopYs);
      }, 100);
    },
    titleClick(index) {
      console.log(index);
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 300);
    },

    contentScroll(position) {
      // console.log(position);
      //1. 获取y值
      const positionY = -position.y;

      //2. positionY和主题中的值进行对比
      // [0, 15918, 17190, 17406, __ob__: Observer]
      // positionY 在0 和15918之间 index = 0
      // positionY 在15918 和 17190之间 index = 1
      // positionY 在17190 和 17406之间 index = 2

      // positionY 大于等于 17406 index = 3

      let length = this.themeTopYs.length;
      for (let i = 0; i < length - 1; i++) {
        if (
          this.currentIndex !== i &&
          positionY > this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }

      this.isShowBackTop = -position.y > 500;

      // 2.决定tabControl是否吸顶（position：fixed）
      this.isTabFixed = -position.y - 44 > this.tabOffsetTop;
    },
    addToCart() {
      //1. 获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      //2. 将商品添加到购物车里
      // this.$store.cartList.push(product)
      // this.$store.commit("addCart", product);

      this.addCart(product).then(res => {
        // this.show = true;
        // this.message = res
        // console.log(res);
        // this.$toast.show(res, 2000)
        this.$toast.show(res, 2000)
        console.log(this.$toast);
        // setTimeout(() => {
        //   this.show = false;
        //   this.message = ''
        // }, 1500)
      })
      // this.$store.dispatch('addCart', product).then(res => {
      //   console.log(res);
      // })
    },
  },
};
</script>

<style lang="css" scoped>
#detail {
  position: relative;
  z-index: 99;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content {
  height: calc(100% - 88px);
}
</style>